package com.nico.voiceview

import android.content.Context
import android.util.AttributeSet
import android.util.DisplayMetrics
import android.util.Log
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.LinearLayout

class ResultEditView : FrameLayout {

    private lateinit var contentEditText: EditText
    private var screenWidth: Int = 0
    private var screenHeight: Int = 0
    constructor(context: Context) : this(context,null) {
    }
    constructor(context: Context, attributeSet: AttributeSet?) : this (context, attributeSet,0) {
    }
    constructor(context: Context, attributeSet: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = windowManager.defaultDisplay
        val realDisplayMetrics = DisplayMetrics()
        // 使用 getRealMetrics() 获取包含虚拟导航栏的屏幕尺寸
        display.getRealMetrics(realDisplayMetrics)

        screenWidth = realDisplayMetrics.widthPixels
        screenHeight = realDisplayMetrics.heightPixels
        val inflate =
            LayoutInflater.from(context).inflate(R.layout.view_result_layout, this, false)
        contentEditText = inflate.findViewById<EditText>(R.id.content)
        contentEditText.textSize=30f
        contentEditText.isFocusable = true;
        contentEditText.setFocusableInTouchMode(true);
        addView(inflate, screenWidth-100, LinearLayout.LayoutParams.WRAP_CONTENT)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMeasureSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightMeasureSize = MeasureSpec.getSize(heightMeasureSpec)
        Log.d(javaClass.simpleName, "widthMeasureSize:${widthMeasureSize}  heightMeasureSize:${ heightMeasureSize}   ")
        setMeasuredDimension(widthMeasureSize,heightMeasureSize)
    }

    public fun showContent(content:String){
        contentEditText.setText(content)
    }
}